home *** CD-ROM | disk | FTP | other *** search
/ Steal This CD / steal_this_cd.iso / Chapter 07 - Where the Hackers Are / virc200.exe / {app} / fpu.dpr < prev    next >
Text File  |  2003-03-23  |  8KB  |  338 lines

  1. library fpu;
  2.  
  3. { Sample DLL for ViRC 2.0. This illustrates how to perform tasks externally
  4.   that are difficult to do from a script.
  5.  
  6.   To use this from ViRC:
  7.     $dll(fpu.dll fadd 1.23 4.56)
  8.     $dll(fpu.dll fsub 1.23 4.56)
  9.     $dll(fpu.dll fmul 1.23 4.56)
  10.     $dll(fpu.dll fdiv 1.23 4.56)
  11.     $dll(fpu.dll fsqrt 199)
  12.     $dll(fpu.dll fround 21.6)
  13.     /dll fpu.dll showver
  14.  
  15.   New for pre8 - persistent DLL support, using VSPersist and p_* functions...
  16.     @ $fpuinst = $LoadDLL(fpu.dll)
  17.     $dll($fpuinst fadd 1.23 4.56)
  18.     ...
  19.     FreeDLL $fpuinst
  20.  
  21.   A few aliases are defined by this VSPersist, so you can actually do this:
  22.     @ $fpuinst = $LoadDLL(fpu.dll)
  23.     $fadd(1.23 4.56)
  24.     ...
  25.     FreeDLL $fpuinst
  26. }
  27.  
  28. uses
  29.   SysUtils,
  30.   Classes,
  31.   Windows,
  32.   Math;
  33.  
  34. // exported functions
  35.  
  36. // persistent versions:
  37.  
  38. type
  39.   TDLLExecProc = procedure (X: PChar) stdcall;
  40.   TDLLParseVarsProc = function (T, B: PChar; BS: Integer): Integer stdcall;
  41.   TDLLDirectAliasProc = procedure (T, A, P: PChar) stdcall;
  42.   
  43.   PDLLInitStruct = ^TDLLInitStruct;
  44.   TDLLInitStruct = packed record
  45.     StructVer, StructSize: Word;
  46.     ClientBuild, ClientPre: Word;
  47.     ClientName, DLLToken: packed array[0..15] of Char;
  48.     MainHWND, ClientHWND: HWND;
  49.     // callback routines
  50.     IRCExec: TDLLExecProc;
  51.     Execute: TDLLExecProc;
  52.     ParseVars: TDLLParseVarsProc;
  53.     DirectAlias: TDLLDirectAliasProc;
  54.   end;
  55.  
  56. function VSPersist(Params: PChar; InitStruct: PDLLInitStruct): Integer; stdcall;
  57. begin
  58.   // indicate success
  59.   Result := 1;
  60.  
  61.   // set up the aliases
  62.   with InitStruct^ do
  63.   begin
  64.     DirectAlias(DLLToken, 'fadd', 'fadd');
  65.     DirectAlias(DLLToken, 'fsub', 'fsub');
  66.     DirectAlias(DLLToken, 'fmul', 'fmul');
  67.     DirectAlias(DLLToken, 'fdiv', 'fdiv');
  68.     DirectAlias(DLLToken, 'fsqrt', 'fsqrt');
  69.     DirectAlias(DLLToken, 'fround', 'fround');
  70.   end;
  71. end;
  72.  
  73. function p_fadd(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  74.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  75. var
  76.   p: PChar;
  77.   f1, f2: Extended;
  78. begin
  79.   // tell ViRC we're returning a string
  80.   Result := 1;
  81.  
  82.   // break Buffer at the space
  83.   p := StrScan(Buffer, ' ');
  84.   if p = nil then Exit;
  85.   try
  86.     p^ := #0;
  87.     Inc(p);
  88.     f1 := StrToFloat(string(Buffer));
  89.     f2 := StrToFloat(string(p));
  90.     StrPCopy(Buffer, FloatToStr(f1 + f2));   // copy the result into the buffer
  91.   except
  92.     // set the result to 0 in case of an error
  93.     Buffer[0] := '0';
  94.     Buffer[1] := #0;
  95.   end;
  96. end;
  97.  
  98. function p_fsub(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  99.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  100. var
  101.   p: PChar;
  102.   f1, f2: Extended;
  103. begin
  104.   // tell ViRC we're returning a string
  105.   Result := 1;
  106.  
  107.   // break Buffer at the space
  108.   p := StrScan(Buffer, ' ');
  109.   if p = nil then Exit;
  110.   try
  111.     p^ := #0;
  112.     Inc(p);
  113.     f1 := StrToFloat(string(Buffer));
  114.     f2 := StrToFloat(string(p));
  115.     StrPCopy(Buffer, FloatToStr(f1 - f2));   // copy the result into the buffer
  116.   except
  117.     // set the result to 0 in case of an error
  118.     Buffer[0] := '0';
  119.     Buffer[1] := #0;
  120.   end;
  121. end;
  122.  
  123. function p_fmul(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  124.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  125. var
  126.   p: PChar;
  127.   f1, f2: Extended;
  128. begin
  129.   // tell ViRC we're returning a string
  130.   Result := 1;
  131.  
  132.   // break Buffer at the space
  133.   p := StrScan(Buffer, ' ');
  134.   if p = nil then Exit;
  135.   try
  136.     p^ := #0;
  137.     Inc(p);
  138.     f1 := StrToFloat(string(Buffer));
  139.     f2 := StrToFloat(string(p));
  140.     StrPCopy(Buffer, FloatToStr(f1 * f2));   // copy the result into the buffer
  141.   except
  142.     // set the result to 0 in case of an error
  143.     Buffer[0] := '0';
  144.     Buffer[1] := #0;
  145.   end;
  146. end;
  147.  
  148. function p_fdiv(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  149.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  150. var
  151.   p: PChar;
  152.   f1, f2: Extended;
  153. begin
  154.   // tell ViRC we're returning a string
  155.   Result := 1;
  156.  
  157.   // break Buffer at the space
  158.   p := StrScan(Buffer, ' ');
  159.   if p = nil then Exit;
  160.   try
  161.     p^ := #0;
  162.     Inc(p);
  163.     f1 := StrToFloat(string(Buffer));
  164.     f2 := StrToFloat(string(p));
  165.     StrPCopy(Buffer, FloatToStr(f1 / f2));   // copy the result into the buffer
  166.   except
  167.     // set the result to 0 in case of an error
  168.     Buffer[0] := '0';
  169.     Buffer[1] := #0;
  170.   end;
  171. end;
  172.  
  173. function p_fsqrt(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  174.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  175. var
  176.   f: Extended;
  177. begin
  178.   // tell ViRC we're returning a string
  179.   Result := 1;
  180.   
  181.   try
  182.     f := StrToFloat(string(Buffer));
  183.     StrPCopy(Buffer, FloatToStr(Sqrt(f)));
  184.   except
  185.     Buffer[0] := '0';
  186.     Buffer[1] := #0;
  187.   end;
  188. end;
  189.  
  190. function p_fround(Active: HWND; Channel, Buffer: PChar; BufSize: Integer;
  191.   Silent: Boolean; Reserved: Pointer): Integer; stdcall;
  192. var
  193.   f: Extended;
  194. begin
  195.   // tell ViRC we're returning a string
  196.   Result := 1;
  197.  
  198.   try
  199.     f := StrToFloat(string(Buffer));
  200.     StrPCopy(Buffer, IntToStr(Round(f)));
  201.   except
  202.     Buffer[0] := '0';
  203.     Buffer[1] := #0;
  204.   end;
  205. end;
  206.  
  207. // non-persistent (mIRC compatible) versions:
  208.  
  209. function fadd(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  210. var
  211.   p: PChar;
  212.   f1, f2: Extended;
  213. begin
  214.   Result := 3;       // tell ViRC that the function is returning a string value
  215.   // break Data at the space
  216.   p := StrScan(Data, ' ');
  217.   if p = nil then Exit;
  218.   try
  219.     p^ := #0;
  220.     Inc(p);
  221.     f1 := StrToFloat(string(Data));
  222.     f2 := StrToFloat(string(p));
  223.     StrPCopy(Data, FloatToStr(f1 + f2));   // copy the result into the buffer
  224.   except
  225.     // set the result to 0 in case of an error
  226.     Data[0] := '0';
  227.     Data[1] := #0;
  228.   end;
  229. end;
  230.  
  231. function fsub(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  232. var
  233.   p: PChar;
  234.   f1, f2: Extended;
  235. begin
  236.   Result := 3;
  237.   p := StrScan(Data, ' ');
  238.   if p = nil then Exit;
  239.   try
  240.     p^ := #0;
  241.     Inc(p);
  242.     f1 := StrToFloat(string(Data));
  243.     f2 := StrToFloat(string(p));
  244.     StrPCopy(Data, FloatToStr(f1 - f2));
  245.   except
  246.     Data[0] := '0';
  247.     Data[1] := #0;
  248.   end;
  249. end;
  250.  
  251. function fmul(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  252. var
  253.   p: PChar;
  254.   f1, f2: Extended;
  255. begin
  256.   Result := 3;
  257.   p := StrScan(Data, ' ');
  258.   if p = nil then Exit;
  259.   try
  260.     p^ := #0;
  261.     Inc(p);
  262.     f1 := StrToFloat(string(Data));
  263.     f2 := StrToFloat(string(p));
  264.     StrPCopy(Data, FloatToStr(f1 * f2));
  265.   except
  266.     Data[0] := '0';
  267.     Data[1] := #0;
  268.   end;
  269. end;
  270.  
  271. function fdiv(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  272. var
  273.   p: PChar;
  274.   f1, f2: Extended;
  275. begin
  276.   Result := 3;
  277.   p := StrScan(Data, ' ');
  278.   if p = nil then Exit;
  279.   try
  280.     p^ := #0;
  281.     Inc(p);
  282.     f1 := StrToFloat(string(Data));
  283.     f2 := StrToFloat(string(p));
  284.     StrPCopy(Data, FloatToStr(f1 / f2));
  285.   except
  286.     Data[0] := '0';
  287.     Data[1] := #0;
  288.   end;
  289. end;
  290.  
  291. function fsqrt(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  292. var
  293.   f: Extended;
  294. begin
  295.   Result := 3;
  296.   try
  297.     f := StrToFloat(string(Data));
  298.     StrPCopy(Data, FloatToStr(Sqrt(f)));
  299.   except
  300.     Data[0] := '0';
  301.     Data[1] := #0;
  302.   end;
  303. end;
  304.  
  305. function fround(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  306. var
  307.   f: Extended;
  308. begin
  309.   Result := 3;
  310.   try
  311.     f := StrToFloat(string(Data));
  312.     StrPCopy(Data, IntToStr(Round(f)));
  313.   except
  314.     Data[0] := '0';
  315.     Data[1] := #0;
  316.   end;
  317. end;
  318.  
  319. function showver(Main, Active: HWND; Data, Params: PChar; Show, NoPause: BOOL): Integer; stdcall;
  320. const
  321.   DLLVer = 'fpu.dll demo by Jesse McGrew, version 1.2, 30 October 2002';
  322.   Advert = ' (http://www.visualirc.net)';
  323. begin
  324.   Result := 2;    // the function returns a command to be run
  325.   // running under ViRC?
  326.   if StrLComp(Params, 'ViRC ', 5) = 0 then
  327.     StrCopy(Data, 'MessageBox ' + DLLVer)
  328.   else
  329.     StrCopy(Data, '/echo 4 ' + DLLVer + Advert);
  330. end;
  331.  
  332. exports
  333.   VSPersist, p_fadd, p_fsub, p_fmul, p_fdiv, p_fsqrt, p_fround,
  334.   fadd, fsub, fmul, fdiv, fsqrt, fround, showver;
  335.  
  336. begin
  337. end.
  338.